﻿$(document).ready(function () {
    page.init();
});

var page = {
    pageIndex: 0,
    pageSize: 10,
    url: "/handler/reward.ashx?action=",
    editor: null,
    tabsLength: 3,

    init: function () {
        $(".top-bar .right li:eq(0)").addClass("current");
        $("#btnSubmit").bind("click", page.submit);

        page.tabsLength = $(".tabs dd").length;

        if (page.tabsLength > 2) {
            KindEditor.ready(function (K) {
                page.editor = K.create('textarea[id="description"]', {
                    minWidth: 600,
                    items: [
                        'source', '|', 'undo', 'redo', '|', 'preview', 'template', 'cut', 'copy', 'paste',
                        'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                        'justifyfull', 'insertorderedlist', 'insertunorderedlist',
                        'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
                        'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                        'italic', 'underline', 'lineheight', 'removeformat', '|', 'image',
                        'table', 'hr', 'emoticons', 'link', 'unlink'
                    ],
                    uploadJson: "/handler/upload.ashx?action=uploadFile"
                });
            });
        }

        $(".tabs dd").click("click", function () {
            $(this).parent().find("dd").removeClass("active");
            $(this).addClass("active");

            var index = $(this).index();
            $("div[id^=tab]").hide();
            $("#tab" + index).show();

            if (index == page.tabsLength - 1 || index == page.tabsLength - 2) {
                $("#tab1").show();
                page.showResponse(0);
            }
        });
        
        $(".tabs dd:eq(0)").addClass("active").click();
    },

    submit: function () {
        var description = page.editor.html();
        if (description == "") {
            alert("请输入创意内容");
            page.editor.focus();
            return false;
        }

        $.post(page.url + "postResponse", { id: page.id, description: description }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            page.editor.html('');
            $(".tabs dd")[1].click();
        }, "json");
    },

    showResponse: function (pageIndex) {
        if (pageIndex != undefined)
            page.pageIndex = pageIndex;
        $.get(page.url + "response", { my: $(".tabs dd.active").index() == page.tabsLength - 1, id: page.id, pageIndex: page.pageIndex, pageSize: page.pageSize, r: Math.random() }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            $("#result").html(TrimPath.processDOMTemplate("responselist", data));
            page.pager(data.count);
            $("#responseCount").html(data.count);
        }, "json");
    },

    pager: function (count) {
        if (count == 0) {
            $("#pagination").empty();
            return;
        }
        $("#pagination").pagination(count, {
            num_edge_entries: 2,
            num_display_entries: 10,
            current_page: page.pageIndex,
            items_per_page: page.pageSize,
            callback: page.showResponse
        });
    }
};

